package com.netsdk.lht2.handle.support;


import com.netsdk.demo.util.DateUtil;
import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.enumeration.EM_TRIGGER_TYPE;
import com.netsdk.lib.structure.DEV_EVENT_TRAFFIC_NONMOTOR_INFO;
import com.netsdk.lib.structure.NET_TIME_EX;
import com.sun.jna.Pointer;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/*
 * 交通非机动车事件检测
 * */
public class EVENT_IVS_TRAFFIC_NONMOTOR_HANDLE extends EventHandle {

    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
        System.out.println("交通非机动车事件检测, code = " + dwAlarmType);
        DEV_EVENT_TRAFFIC_NONMOTOR_INFO msg = new DEV_EVENT_TRAFFIC_NONMOTOR_INFO();
        ToolKits.GetPointerData(pAlarmInfo, msg);
        // 通道号
        String nChannelID = getChannelMapping(msg.nChannelID);
        System.out.println("nChannelID:" + msg.nChannelID);
        // 动作
        int nAction = msg.nAction;
        System.out.println("nAction:" + nAction);
        // 规则
        int nRuleId = msg.nRuleId;
        System.out.println("nRuleId:" + nRuleId);
        // 触发方式
        int emTriggerType = msg.emTriggerType;
        System.out.println("emTriggerType:" + emTriggerType + "," + EM_TRIGGER_TYPE.getNoteByValue(emTriggerType));
        //事件名称
        byte[] szName = msg.szName;
        System.out.println("szName UTF-8:" + new String(szName, "UTF-8").trim());
        System.out.println("szName GBK:" + new String(szName, "GBK").trim());
        //相对事件时间戳(单位是毫秒)
        double pts = msg.PTS;
        System.out.println("pts:" + new Date((long) pts));
        //UTC时间
        NET_TIME_EX UTC = msg.UTC;
        System.out.println("UTC:" + UTC.toStringTime());
        //事件id
        int nEventID = msg.nEventID;
        System.out.println("nEventID:" + nEventID);
        EventDto dto = new EventDto();
        //图片保存
        if (pBuffer != null && dwBufSize > 0) {
            System.out.println("basic picture received!");
            String pName = System.currentTimeMillis() + "related_TrafficNoMotor_basic.jpg";
            String bigPicture = picturePath + "\\" + pName;
            if (null != pBuffer) {
                dto.setBigPicture(pName);
            }
            ToolKits.savePicture(pBuffer, dwBufSize, bigPicture);
        }
        //图片保存
        if (pBuffer != null) {
            String pName = System.currentTimeMillis() + "related_EVENT_IVS_DREGS_UNCOVERED.jpg";
            String picture = picturePath + "/" + pName;
            if (null != pBuffer) {
                dto.setImg_name(pName);
            }
            ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
        }
        if (pBuffer != null && dwBufSize > 0 && msg.stuNonMotor.stuSceneImage.nLength > 0) {
            System.out.println("Scene picture received!");
            String pName = System.currentTimeMillis() + "related_TrafficNoMotor_scene.jpg";
            String scenePicture = picturePath + "\\" + pName;
            if (null != pBuffer) {
                dto.setScenePicture(pName);
            }
            ToolKits.savePicture(pBuffer, msg.stuNonMotor.stuSceneImage.nOffSet, msg.stuNonMotor.stuSceneImage.nLength, scenePicture);
        }
        if (pBuffer != null && dwBufSize > 0 && msg.stuNonMotor.stuFaceSceneImage.nLength > 0) {
            System.out.println("face picture received!");
            String pName = System.currentTimeMillis() + "related_TrafficNoMotor_face.jpg";
            String facePicture = picturePath + "\\" + pName;
            if (null != pBuffer) {
                dto.setFacePicture(pName);
            }
            ToolKits.savePicture(pBuffer, msg.stuNonMotor.stuFaceSceneImage.nOffSet, msg.stuNonMotor.stuFaceSceneImage.nLength, facePicture);
        }
        //图片保存
        if (pBuffer != null && dwBufSize > 0 && msg.stuCommInfo.nPictureNum > 0) {
            System.out.println(msg.stuCommInfo.nPictureNum + " pictures received!");
            for (int i = 0; i < msg.stuCommInfo.nPictureNum; i++) {
                String pName = System.currentTimeMillis() + "related_TrafficJam_common_" + i + ".jpg";
                String bigPicture = picturePath + "\\" + pName;
                if (null != pBuffer) {
                    dto.setBigPicture(StringUtils.isNotEmpty(dto.getBigPicture()) ? dto.getBigPicture() + "," + pName : "" + pName);
                }
                ToolKits.savePicture(pBuffer, msg.stuCommInfo.stuPicInfos[i].nOffset, msg.stuCommInfo.stuPicInfos[i].nLength, bigPicture);
            }
        }

        dto.setEvent_name("交通非机动车事件检测");
        dto.setEvent_code(dwAlarmType + "");
        dto.setChannel(nChannelID);
        dto.setnAction(nAction + "");
        dto.setRule_Id(nRuleId + "");
        dto.setEvent_utc_time(UTC.toStringTime());
        dto.setSzName(new String(msg.szName, "UTF-8").trim());
        dto.setTriggertType(emTriggerType + "");
        dto.setEvent_time(DateUtil.format(new Date((long) pts), "yyyy-MM-dd HH:mm:ss.SSS"));
        dto.setEvent_id(nEventID + "");
        dto.setEvent_utc_time(UTC.toStringTime());
        // 数据入库
        insertEventDto(dto);
    }
}
